package rest

import (
	"github.com/labstack/echo/v4"

	"bmember/internal/service"

	pb "bmember/api/pb/server"
)

type aHandler struct {
	*Base
	Name string
}

// NewAssetHandler 资产相关handler
func NewAssetHandler() *aHandler {
	return &aHandler{
		Base: base,
		Name: "member_related_handler",
	}
}

// Register 路由
func (hl *aHandler) Register(r *echo.Group) {
	// 积分赠送
	r.POST("/v1/integral/award", hl.integralAward)
	// 积分记录
	r.GET("/v1/integral/records", hl.integralRecords)
	// 充值单创建
	r.POST("/v1/recharge-order/create", hl.createRechargeOrder)
	// 根据单号获取充值单详情
	r.GET("/v1/recharge-orders/:order_no", hl.getRechargeOrderByOrderNo)
	// 创建充值订单，支付订单
	r.POST("/v1/recharge-order/pay", hl.payRechargeOrder)
	// 创建充值订单，支付订单, 支付成功
	r.POST("/v1/recharge-order/pay/success", hl.triggerPaySuccess)
	// 取消支付订单
	r.POST("/v1/recharge-order/pay/cancel", hl.cancelPayOrder)
	// 余额流水记录
	r.GET("/v1/balance/records", hl.balanceRecords)
}

// 积分赠送
func (hl *aHandler) integralAward(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	req := &pb.IntegralAwardReq{}
	if errL = hl.ParseBody(c, req); errL != nil {
		return errL
	}
	ret, errL := service.GetAssetSrv().IntegralAward(ctx, token.BrandId, req, token.EmpId)
	return hl.SimpleRet(c, ret, errL)
}

func (hl *aHandler) integralRecords(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.VerifyJwtToken(c)
	if errL != nil {
		return errL
	}
	// 接口支付分页
	pg := paginationParse(c)
	ret, errL := service.GetAssetSrv().GetIntegralRecords(ctx, token.BrandID, token.MemberID, pg)
	return hl.SimpleRet(c, ret, errL)
}

func (hl *aHandler) createRechargeOrder(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.VerifyJwtToken(c)
	if errL != nil {
		return errL
	}
	req := &pb.CreateRechargeOrderReq{}
	if errL = hl.ParseBody(c, req); errL != nil {
		return errL
	}
	ret, errL := service.GetAssetSrv().CreateRechargeOrder(ctx, token.MemberID, req, token.BilinApp)
	return hl.SimpleRet(c, ret, errL)
}

func (hl *aHandler) getRechargeOrderByOrderNo(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.VerifyJwtToken(c)
	if errL != nil {
		return errL
	}
	orderNo := c.Param("order_no")
	ret, errL := service.GetAssetSrv().GetRechargeOrderByOrderNo(ctx, token.MemberID, orderNo)
	return hl.SimpleRet(c, ret, errL)
}

func (hl *aHandler) payRechargeOrder(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.VerifyJwtToken(c)
	if errL != nil {
		return errL
	}
	req := &pb.PrepayInfoReq{}
	if errL = hl.ParseBody(c, req); errL != nil {
		return errL
	}
	ret, errL := service.GetAssetSrv().CreatePayOrderForRechargeOrder(ctx, token.AppID, token.OpenID, req)
	return hl.SimpleRet(c, ret, errL)
}

func (hl *aHandler) triggerPaySuccess(c echo.Context) error {
	ctx := c.Request().Context()
	_, errL := hl.VerifyJwtToken(c)
	if errL != nil {
		return errL
	}
	req := &pb.PayOrderReq{}
	if errL = hl.ParseBody(c, req); errL != nil {
		return errL
	}
	errL = service.GetAssetSrv().TriggerPayOrderSuccess(ctx, req.PayOrderNo)
	return hl.SimpleRet(c, nil, errL)
}

func (hl *aHandler) cancelPayOrder(c echo.Context) error {
	ctx := c.Request().Context()
	_, errL := hl.VerifyJwtToken(c)
	if errL != nil {
		return errL
	}
	req := &pb.PayOrderReq{}
	if errL = hl.ParseBody(c, req); errL != nil {
		return errL
	}
	errL = service.GetAssetSrv().CancelPayOrder(ctx, req.PayOrderNo)
	return hl.SimpleRet(c, nil, errL)
}

func (hl *aHandler) balanceRecords(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.VerifyJwtToken(c)
	if errL != nil {
		return errL
	}
	// 接口支付分页
	pg := paginationParse(c)
	ret, errL := service.GetAssetSrv().GetBalanceRecords(ctx, token.BrandID, token.MemberID, pg)
	return hl.SimpleRet(c, ret, errL)
}
